//
//  CarLogViewController.m
//  Rail
//
//  Created by hychen on 2018/3/3.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "CarLogViewController.h"
#import "CarLogTableViewCell.h"
#import "AddNewCarLogViewController.h"
#import "CarDetailViewController.h"

@interface CarLogViewController ()<EVNCustomSearchBarDelegate>
@property (nonatomic, copy) NSString *searchText;
@property (nonatomic, strong) EVNCustomSearchBar *searchView;
@end

@implementation CarLogViewController


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [IQKeyboardManager sharedManager].enable = NO;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [IQKeyboardManager sharedManager].enable = YES;
}


- (void)setupSearchView {
    self.searchView = [[EVNCustomSearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 44)];
    self.searchView.backgroundColor = Color_NavBgColor;
    self.searchView.iconAlign = EVNCustomSearchBarIconAlignCenter;
    self.searchView.iconImage = [UIImage imageNamed:@"EVNCustomSearchBar.bundle/searchImageBlack"];
    self.searchView.placeholder = @"搜索";
    self.searchView.delegate = self;
    self.searchView.isHiddenCancelButton = NO;
    [self.view addSubview:self.searchView];
    [self.searchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.view);
        make.height.mas_equalTo(45);
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.navigationItem addSimpleTitleView:@"车辆登记"];
    [self.navigationItem addRightNavItem:self normalImage:[UIImage imageNamed:@"app_title_bar_add_n"] highlightedImage:[UIImage imageNamed:@"app_title_bar_add_p"] selector:@selector(clickAddNewCarLog)];
    self.isOpenFooter = YES;
    
    [self addNotifications];
    
    [self setupSearchView];
    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(self.searchView.mas_bottom);
    }];
    
    [self.tableView.mj_header beginRefreshing];
}

- (void)addNotifications {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(carLogoutSuccess) name:Notify_Car_LogOut_Success object:nil];
}

- (void)carLogoutSuccess {
    [self.tableView.mj_header beginRefreshing];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//跳转新增进岗
- (void)clickAddNewCarLog {
    AddNewCarLogViewController *vc = [AddNewCarLogViewController new];
    BMWeakSelf
    [vc setBlockLoginCarSuccess:^{
        [weakSelf refreshData];
    }];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)refreshData {
    self.currentPage = 1;
    self.isOpenFooter = NO;
    BMWeakSelf
    [HttpRequest loadCarAccessListDataWithQueryDate:@"" pageNum:[NSString stringWithFormat:@"%ld",self.currentPage] areaId:@"" plateNum:self.searchText?:@"" success:^(NSArray *object) {
        if ([object isKindOfClass:[NSArray class]]) {
            weakSelf.dataArray = [NSMutableArray arrayWithArray:object];
            [weakSelf.tableView reloadData];
            if (object.count > 9) {
                weakSelf.isOpenFooter = YES;
            }
            [weakSelf.tableView.mj_footer resetNoMoreData];
        }
        [weakSelf.tableView.mj_header endRefreshing];
    } failure:^(NSString *code, NSString *msg) {
        Show_Msg_Error(msg)
        [weakSelf.tableView.mj_header endRefreshing];
    }];
}

- (void)loadMoreData {
    self.currentPage++;
    BMWeakSelf
    [HttpRequest loadCarAccessListDataWithQueryDate:@"" pageNum:[NSString stringWithFormat:@"%ld",self.currentPage] areaId:@"" plateNum:@"" success:^(NSArray *object) {
        if ([object isKindOfClass:[NSArray class]]) {
            [weakSelf.dataArray addObjectsFromArray:object];
            if (object.count < 1) {
                [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
            }
        }
        [weakSelf.tableView reloadData];
        [weakSelf.tableView.mj_footer endRefreshing];
    } failure:^(NSString *code, NSString *msg) {
        Show_Msg_Error(msg)
        [weakSelf.tableView.mj_footer endRefreshing];
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 70.f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CarAccessModel *model = [self.dataArray objectAtIndex:indexPath.row];
    CarLogTableViewCell *cell = [CarLogTableViewCell cellWithTableView:tableView class:[CarLogTableViewCell class]];
    cell.model = model;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    //跳转车辆详情
    CarAccessModel *model = [self.dataArray objectAtIndex:indexPath.row];
    if ([model isKindOfClass:[CarAccessModel class]]) {
        CarDetailViewController *vc = [CarDetailViewController new];
        vc.model = model;
        [self.navigationController pushViewController:vc animated:YES];
    }

}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self.view endEditing:YES];
}

- (void)searchBarTextDidEndEditing:(EVNCustomSearchBar *)searchBar {
    self.searchText = searchBar.text;
    [self.tableView.mj_header beginRefreshing];
}

- (void)searchBarSearchButtonClicked:(EVNCustomSearchBar *)searchBar {
    self.searchText = searchBar.text;
    [self.tableView.mj_header beginRefreshing];
}

- (void)searchBar:(EVNCustomSearchBar *)searchBar textDidChange:(NSString *)searchText {
    if ([searchText isEqualToString:@""]) {
        self.searchText = searchText;
        [self refreshData];
    }
}

- (void)searchBarCancelButtonClicked:(EVNCustomSearchBar *)searchBar {
    [self.searchView resignFirstResponder];
}
@end

